package com.fx.ruankao.service.impl;

import com.fx.ruankao.entity.Permission;
import com.fx.ruankao.mapper.PermissionMapper;
import com.fx.ruankao.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * 权限服务实现类
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    
    @Autowired
    private PermissionMapper permissionMapper;
    
    @Override
    public int insert(Permission permission) {
        return permissionMapper.insert(permission);
    }
    
    @Override
    public int deleteById(Integer id) {
        return permissionMapper.deleteById(Long.valueOf(id));
    }
    
    @Override
    public int update(Permission permission) {
        return permissionMapper.update(permission);
    }
    
    @Override
    public Permission selectById(Integer id) {
        return permissionMapper.selectById(Long.valueOf(id));
    }
    
    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectList(new Permission());
    }
    
    @Override
    public Permission selectByPermissionName(String permissionName) {
        Permission permission = new Permission();
        permission.setPermissionName(permissionName);
        List<Permission> list = permissionMapper.selectList(permission);
        return list.isEmpty() ? null : list.get(0);
    }
    
    @Override
    public Permission selectByPermissionCode(String permissionCode) {
        return permissionMapper.selectByPermissionCode(permissionCode);
    }
    
    @Override
    public List<Permission> selectByRoleId(Integer roleId) {
        return permissionMapper.selectPermissionsByRoleId(Long.valueOf(roleId));
    }
    
    @Override
    public List<Permission> selectByUserId(Integer userId) {
        return permissionMapper.selectPermissionsByUserId(Long.valueOf(userId));
    }
    
    @Override
    public int batchDelete(Integer[] ids) {
        int count = 0;
        if (ids != null && ids.length > 0) {
            for (Integer id : ids) {
                count += permissionMapper.deleteById(Long.valueOf(id));
            }
        }
        return count;
    }
    
    @Override
    public int countPermissions() {
        return permissionMapper.selectCount(new Permission());
    }
    
    @Override
    public List<Permission> selectPermissionTree() {
        List<Permission> permissions = permissionMapper.selectList(new Permission());
        // 构建权限树
        return buildPermissionTree(permissions, 0L);
    }
    
    /**
     * 构建权限树
     * @param permissions 所有权限列表
     * @param parentId 父节点ID
     * @return 权限树结构
     */
    private List<Permission> buildPermissionTree(List<Permission> permissions, Long parentId) {
        List<Permission> tree = new ArrayList<>();
        Map<Long, List<Permission>> permissionMap = new HashMap<>();
        
        // 按父节点ID分组
        for (Permission permission : permissions) {
            Long pid = permission.getParentId();
            if (pid == null) {
                pid = 0L;
            }
            if (!permissionMap.containsKey(pid)) {
                permissionMap.put(pid, new ArrayList<>());
            }
            permissionMap.get(pid).add(permission);
        }
        
        // 递归构建树
        buildTree(permissionMap, tree, parentId);
        
        return tree;
    }
    
    /**
     * 递归构建树结构
     */
    private void buildTree(Map<Long, List<Permission>> permissionMap, List<Permission> tree, Long parentId) {
        List<Permission> children = permissionMap.get(parentId);
        if (children != null) {
            for (Permission child : children) {
                tree.add(child);
                // 递归添加子节点
                List<Permission> subTree = new ArrayList<>();
                buildTree(permissionMap, subTree, child.getId());
                if (!subTree.isEmpty()) {
                    // Permission实体类没有children字段，不设置子节点
                }
            }
        }
    }
}